package com.guimin.agent.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guimin.agent.dto.AddAgentDto;
import com.guimin.agent.entity.GuiminAgent;
import com.guimin.agent.service.GuiminAgentService;
import com.guimin.user.dto.LoginInfoDto;
import com.guimin.user.entity.UserInfo;
import com.guimin.user.service.UserInfoService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * (GuiminAgent)表控制层
 *
 * @author makejava
 * @since 2021-11-17 10:11:03
 */
@RestController
@RequestMapping("/agent")
public class GuiminAgentController {
    /**
     * 服务对象
     */
    @Resource
    private GuiminAgentService guiminAgentService;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private UserInfoService userInfoService;
    /**
     * 获取站长列表
     *
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/list")
    public R<IPage<GuiminAgent>> getAgentList(@RequestParam(defaultValue = "1") Integer current,
                                              @RequestParam(defaultValue = "10") Integer size) {
        IPage<GuiminAgent> page = guiminAgentService.page(new Page<>(current, size));
        return R.ok(page);
    }

    /**
     * 根据站长id获取站长信息
     *
     * @param agentId
     * @return
     */
    @GetMapping("/get")
    public R<GuiminAgent> getAgentInfo(@RequestParam Long agentId) {
        GuiminAgent agent = guiminAgentService.getById(agentId);
        return agent == null ? R.failed("查无此人") : R.ok(agent);
    }

    /**
     * 添加站长
     *
     * @return
     */
    @PostMapping("/add")
    public R<String> addAgent(@RequestBody AddAgentDto dto, HttpServletRequest request) {
        System.out.println(dto);
        LoginInfoDto userInfo= (LoginInfoDto) redisTemplate.opsForValue().get("token"+request.getHeader("token"));
        //生成站长账户
        if (userInfoService.count(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getPhone, dto.getAgentPhone())) == 0) {
            if (null == userInfoService.register(new LoginInfoDto().setPhone(dto.getAgentPhone()).setRole("AGENT"))) {
                return R.failed("添加失败");
            }
        }
        boolean save = guiminAgentService.save(new GuiminAgent()
                .setAgentName(dto.getAgentName())
                .setAgentPhone(dto.getAgentPhone())
                .setUserId(userInfo.getId())
        );

        return save ? R.ok(null) : R.failed("添加失败");
    }

    /**
     * 更新站长
     *
     * @return
     */
    @PutMapping("/update")
    public R<String> updateAgent(@RequestBody GuiminAgent dto) {
        dto.setUserId(null);
        boolean update = guiminAgentService.updateAgent(dto);
        return update ? R.ok(null) : R.failed("修改失败");
    }

    /**
     * 删除站长
     *
     * @return
     */
    @DeleteMapping("/del")
    public R<String> delAgent(@RequestParam Integer agentId) {
        boolean del = guiminAgentService.remove(new LambdaQueryWrapper<GuiminAgent>().eq(GuiminAgent::getAgentId,agentId));
        return del ? R.ok(null) : R.failed("删除失败");
    }


}

